﻿using System;
using System.Web.Mvc;
using Xoohoo.Models.ViewModels;

namespace Xoohoo.Extensions
{
    public static class PromptViewModelExtensions
    {
        public static void AddModelErrors(this PromptViewModel prompt, ModelErrorCollection errors)
        {
            if (errors == null)
                throw new ArgumentNullException("errors");

            foreach (var error in errors)
            {
                if (!error.ErrorMessage.IsNullOrWhiteSpace())
                    prompt.AddMessage(error.ErrorMessage);
                if (error.Exception != null)
                    prompt.AddMessage(error.Exception.Message);
            }
        }
        public static void AddModelErrors(this PromptViewModel prompt, ModelStateDictionary modelStates)
        {
            if (modelStates == null)
                throw new ArgumentNullException("modelStates");

            if (modelStates.Values == null)
                throw new ArgumentException("modelStates.Values is null.", "modelStates");

            foreach (var modelState in modelStates.Values)
            {
                prompt.AddModelErrors(modelState.Errors);
            }
        }
    }
}
